
let currentPage = 1;

$(".fa-search").click(function(){
	queryAllDanMu()
})

$("#danmakuList").on("click",".delete",function(){
	if($("#danmakuList tr").length < 2){
		currentPage = 1
	}
	deleteDanmakuB($(this).find("input").val(),currentPage)
})

queryAllDanMu()
function queryAllDanMu(pageNum = 1){
	let content = $("#searchInput").val()
	let danmakuList = $("#danmakuList")
	$.ajax({
		url:"queryAllDanmakuB",
		type:"post",
		data:{
			content:content,
			pageNum:pageNum
		},
		success:function(data){
			danmakuList.empty()
			if(data.code == 200){
				let danmakus = data.data.data
				for(let i = 0;i<danmakus.length;i++){
					let danmaku = danmakus[i]
					let totalTime = countVideoTime(danmaku.playTime)
					let tr = `
						<tr>
							<td></td>
		                    <td>
		                        <div class="danmaku-text">${danmaku.content}</div>
		                    </td>
		                    <td>
		                        <div class="danmaku-info">
		                            <span><i class="fas fa-user"></i> ${danmaku.userName}</span>
		                            <span><i class="fas fa-video"></i> ${danmaku.videoTitle}</span>
		                            <span><i class="fas fa-clock"></i> ${totalTime}</span>
		                        </div>
		                    </td>
		                    <td>
		                        
		                    </td>
		                    <td class="action-cell">
		                        <div class="icon-btn delete-btn delete">
		                        	<input type="hidden" value="${danmaku.danmakuId}">
		                            <i class="fas fa-trash"></i>
		                        </div>
		                    </td>
						</tr>
					`
					danmakuList.append(tr)
				}
				makePageBar(data.data)
				$(".pagination").css("display","flex")
			}else{
				let div = `
					<div style="width: 148px;margin: 170% 396%;">
	                	<img src="img/ZFZJ.svg">
	                	<p style="text-align: center">暂时没有弹幕哦~</p>
	                </div>
				`
				danmakuList.append(div)
				$(".pagination").css("display","none")
			}
		}
	})
};

function deleteDanmakuB(danmakuId,page){
	$.ajax({
		url:"deleteDanmakuB",
		type:"post",
		data:{
			danmakuId:danmakuId
		},
		success:function(data){
			if(data.code == 200){
				queryAllDanMu(page)
				promptAnimation("删除弹幕成功")
			}else{
				promptAnimation("删除弹幕失败")
			}
		}
	})
}

function makePageBar(page){
	$(".batch-select input").prop("checked",false)
    let pageBar = $(".pagination");
    let navigatorNums = page.totalPageNum;
    pageBar.empty();
    
    let prevPage = $("<button class='page-btn'><i class='fas fa-chevron-left'></i></button>").click(function() {
    	queryAllDanMu(currentPage - 1);
        currentPage--
    });
    pageBar.append(prevPage);

    for (let i = 0; i < navigatorNums.length; i++) {
        let num = navigatorNums[i];
        let pageLink = $("<button class='page-btn'>"+num+"</button>").click(function() {
        	queryAllDanMu(num);
            currentPage = num 
        });
        if (num == currentPage) {
            pageLink.addClass("active");
        }
        pageBar.append(pageLink);
    }
    

    let start = (page.pageNum - 1) * page.pageSize + 1;
    let end = page.pageNum * page.pageSize;
    end = end > page.totalRows ? page.totalRows : end;
    let bottomPage = $("<span class='page-info'>"+`${start}`+"-"+`${end}`+" 条，共 "+`${page.totalRows}`+"条</div>");
    pageBar.append(bottomPage)
    
    let nextPage = $("<button class='page-btn'><i class='fas fa-chevron-right'></i></button>").click(function() {
    	queryAllDanMu(currentPage + 1);
        currentPage++
    });
    pageBar.append(nextPage);
    

    if (!page.hasPrev) {
    	prevPage.prop("disabled",true)
    }
    if (!page.hasNext) {
    	nextPage.prop("disabled",true)
    }
}

function countVideoTime(totalSecond){
	let hous = parseInt(totalSecond/60/60)
	let minute = parseInt((totalSecond - hous*60*60)/60)
	let second = totalSecond - hous*60*60 - minute*60
	if(hous < 10){
		hous = "0"+hous
	}
	if(minute < 10){
		minute = "0"+minute
	}
	if(second < 10){
		second = "0"+second
	}
	let totalTime;
	if(hous == 0){
		totalTime = "00:"+minute+":"+second
	}else{
		totalTime = hous+":"+minute+":"+second
	}
	return totalTime;
}

function promptAnimation(text) {
    let $prompt = $("#prompt");
    $prompt.text(text);
    
    // 新增：立即停止所有正在进行的动画并重置位置
    $prompt.stop(true, true).css("top", "-60px");

    // 清除之前的定时器（防止动画堆积）
    if ($prompt.data('timeoutId')) {
        clearTimeout($prompt.data('timeoutId'));
    }

    // 上升动画
    $prompt.animate({ top: 0 }, {
        duration: 240,
        easing: "linear",
        complete: function() {
            // 保存新的定时器ID到元素数据
            const timeoutId = setTimeout(function() {
                // 下降动画（同样添加中断机制）
                $prompt.stop(true, true).animate({ top: -60 }, {
                    duration: 240,
                    easing: "linear",
                    complete: function() {
                        // 动画完成后清除定时器ID
                        $prompt.removeData('timeoutId');
                        if($("#videoDetailModal").hasClass("active")){
                        	closeVideoDetail()
                        }
                    }
                });
            }, 1500);
            
            // 存储定时器ID用于后续清除
            $prompt.data('timeoutId', timeoutId);
        }
    });
}